﻿module Sustainalytics.Accounts.Admin {

    import Common = Sustainalytics.Common;
    import CommonAdmin = Sustainalytics.Common.Admin;

    export interface IAccountsController extends ng.IScope {
        getAccounts: any;
        accountsList: AccountsDto[];
        status: any;
        count: any;
        orderby: string;
        descending: boolean;
        pageSize: any;
        setOrder: any;
        range: any;
        displayedItems: any;
        accountsForm: any;
        statusChange: any;
        checkSortBy: (column: string, direction: boolean) => boolean;
        lastSyncWithCRM: Date;
    }

    export class AccountsController {

        constructor(
            private $scope: IAccountsController,
            $rootScope,
            notificationFactory: Common.INotificationFactory,
            staticObjectsAdmin: CommonAdmin.IStaticObjects,
            accountFactory: AccountsService) {

            $scope.getAccounts = (resetStatus) => {
                if (resetStatus) {
                    $scope.status = 'All';
                }
                var listOptions = new CommonAdmin.HttpGetOptionsDto();

                // get pager
                var pager: Sustainalytics.Pager.PagerController = $scope['pager'];

                // If pager is not defined then abort execution.
                // Current controller is created before Pager controller.
                if (!pager) {
                    $rootScope.loading = false;
                    return;
                }

                listOptions.skip = pager.pagingOptions.pageSize * (pager.pagingOptions.pageIndex - 1);
                listOptions.take = pager.pagingOptions.pageSize;
                listOptions.orderBy = [$scope.orderby];
                listOptions.orderAscending = !$scope.descending;

                accountFactory.accountsGetList(listOptions, $scope.status, $scope.accountsForm.searchText)
                    .then(data => {
                    $scope.accountsList = data;
                    $scope.count = data.length;
                });

                accountFactory.accountsGetLastCrmSync()
                    .then((data) => {
                        $scope.lastSyncWithCRM = new Date(data);
                    },
                    (reason) => { notificationFactory.error(reason, "Error getting lastSyncWithCRM date !");  }
                    );


                accountFactory.accountsGetCount($scope.status, $scope.accountsForm.searchText)
                    .then(data=> {
                    pager.totalItemsCount = data;
                });
            };

            // subscribe to pager events
            $scope.$on('pagerChanged', function (event, pager) {
                // get accounts
                $scope.getAccounts();
            });

            $scope.orderby = 'name';
            $scope.descending = false;
            $scope.status = 'Active';
            $scope.accountsForm = { searchText: '' };
            //paging
            $scope.lastSyncWithCRM = null;

            $scope.setOrder = (orderby) => {
                if (orderby === $scope.orderby) {
                    $scope.descending = !$scope.descending;
                }
                $scope.orderby = orderby;
                $scope.getAccounts(false);
            };
            $scope.checkSortBy = (value, direction) => {
                return $scope.orderby === value && $scope.descending == direction;
            };

            $scope.statusChange = () => {
                $scope.accountsForm.searchText = "";
                $scope.getAccounts();
            };
            $scope.getAccounts();

            $scope.accountsList = [];
        }
    }


    var mod = angular.module(accountAdminModuleName);

    mod.controller('AccountsController',
        ['$scope', '$rootScope', 'notificationFactory', 'staticObjectsAdmin', 'AccountsService', AccountsController]);
}